package org.apache.cordova.geolocation;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import static org.apache.cordova.CordovaActivity.TAG;

/**
 * Created by 席国鹏 on 2020/5/22.
 */
public class HybirdLocation {
    public static boolean Debug = false;
    LocationManager manager = null;
    HBListener listener;
    Context context;
    Location currentLocation = null;
    String provide = "";
    long gpsFlag = 0;
    boolean isStart = false;

    public HybirdLocation(HBListener listener, Context context) {
        this.listener = listener;
        this.context = context;
        initLocation();
    }

    private void initLocation() {
        if (manager == null) {
            manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        }
        Criteria criteria = new Criteria();
        criteria.setCostAllowed(false);//设置位置服务免费
        criteria.setAccuracy(Criteria.ACCURACY_COARSE); //设置水平位置精度
        //设置不需要获取海拔方向数据
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        //设置允许产生资费
        criteria.setCostAllowed(true);
        //要求低耗电
        criteria.setPowerRequirement(Criteria.POWER_HIGH);
        provide = manager.getBestProvider(criteria, false);
    }

    @SuppressLint("MissingPermission")
    public void start() {
        if (isStart) {
            return;
        }
        isStart = true;
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, NetlocationListener);
    }

    public void stop() {
        isStart = false;
        manager.removeUpdates(locationListener);
        manager.removeUpdates(NetlocationListener);
    }

    @SuppressLint("MissingPermission")
    public Location getCurrentLocation() {
        if (currentLocation == null) {
            currentLocation = manager.getLastKnownLocation(provide);
        }
        return currentLocation;
    }

    LocationListener NetlocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.i(TAG, "onLocationChanged: 网络定位 " + location.toString());
            if (System.currentTimeMillis() - gpsFlag > 5000) {//5秒内没有GPS定位，则使用网络定位
                currentLocation = location;
                if(Debug)
                Toast.makeText(context, "网络定位成功", Toast.LENGTH_LONG).show();
                if (listener != null) {
                    listener.location(currentLocation, LocationManager.NETWORK_PROVIDER);
                }
            } else {
                if(Debug)
                Toast.makeText(context, "网络定位成功-不使用", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG, "onLocationChanged: " + location.toString());
            currentLocation = location;
            if (listener != null) {
                listener.location(currentLocation, LocationManager.GPS_PROVIDER);
            }
            gpsFlag = System.currentTimeMillis();
            if(Debug)
            Toast.makeText(context, "GPS定位成功", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };


    public interface HBListener {
        void location(Location location, String type);
    }
}
